#include "../main.h"

/* 
--------------------------------------------------------------------------------
	函数功能：获取联网设备
--------------------------------------------------------------------------------
*/
int device_get_list(int sockfd, char *tagid)
{
	FILE *fp = NULL;
	char *ptr = NULL;
	int ret = 0;
	int count = 0;
	char ip[16];
	char mac[20];
	char name[64];
	unsigned int time;
	unsigned int upspeed;
	unsigned int dwspeed;
	char uplimit[12];
	char dwlimit[12];
	int black = 0;
	int online = 0;

	char buf[1024];

	cJSON *pRow = NULL, *pRank = NULL, *pRoot = NULL;

	pRow = cJSON_CreateArray();

	system("ps | grep ipbandwidth.sh | grep -v grep >/dev/null 2>&1 || /usr/sbin/ipbandwidth.sh &");

	if ((fp = fopen(DEVFILE, "r")) != NULL)
	{
		memset(buf, 0, sizeof(buf));
		while (fgets(buf, sizeof(buf), fp) != NULL)
		{
			if (buf[strlen(buf) - 1] == '\r' || buf[strlen(buf) - 1] == '\n')
				buf[strlen(buf) - 1] = '\0';

			time = 0;
			black = 0;
			online = 0;
			upspeed = 0;
			dwspeed = 0;
			memset(ip, 0, sizeof(ip));
			memset(mac, 0, sizeof(mac));
			memset(name, 0, sizeof(name));
			memset(uplimit, 0, sizeof(uplimit));
			memset(dwlimit, 0, sizeof(dwlimit));

			count = 1;

			while ((ptr = strrchr(buf, '|')) != NULL)
			{
				*ptr++ = 0;

				if (count == 1)
					black = atoi(ptr);
				else if (count == 4)
					dwspeed = atoi(ptr);
				else if (count == 5)
					upspeed = atoi(ptr);
				else if (count == 7)
					online = atoi(ptr);
				else if (count == 8)
					memcpy(name, ptr, sizeof(name));
				else if (count == 9)
					time = atoi(ptr);
				else if (count == 10)
					memcpy(mac, ptr, sizeof(mac));
				else if (count == 11)
					memcpy(ip, ptr, sizeof(ip));

				count++;
			}
			memcpy(ip, buf, sizeof(ip));

			if (online)
			{
				cJSON_AddItemToArray(pRow, pRoot = cJSON_CreateObject());
				cJSON_AddStringToObject(pRoot, "name", name);
				cJSON_AddStringToObject(pRoot, "ip", ip);
				cJSON_AddStringToObject(pRoot, "macAddress", mac);
				cJSON_AddNumberToObject(pRoot, "loginTime", time);

				black = device_get_access(mac);
				cJSON_AddNumberToObject(pRoot, "access", black);

				cJSON_AddItemToObject(pRoot, "upAndDownSize", pRank = cJSON_CreateObject());
				cJSON_AddNumberToObject(pRank, "up", upspeed);
				cJSON_AddNumberToObject(pRank, "down", dwspeed);
			}
			//		printf("### pRoot ### %s \n", cJSON_Print(pRoot));

			memset(buf, 0, sizeof(buf));
		}
		fclose(fp);
	}

	//	printf("### pRow ### %s \n", cJSON_Print(pRow));

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "DEVICE_NET_DEVICE_REP");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_NET_DEVICE_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	//	printf("### pRoot ### %s \n", cJSON_Print(pRoot));

	char *buffer = cJSON_PrintUnformatted(pRoot);

	if (pRow != NULL)
	{
		cJSON_Delete(pRow);
	}

	if (pRoot != NULL)
	{
		cJSON_Delete(pRoot);
	}

	ret = send_msg(sockfd, buffer, strlen(buffer));

	delete_mem(buffer);

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：查询是否为黑名单
--------------------------------------------------------------------------------
*/
int device_get_access(char *mac)
{
	int ret = 0;
	int count = 0;
	int enable = 0;
	int access = 1;
	char cmd[128];
	char *enable_result = get_value_change("filter.list.enable");
	if (char_has_value(enable_result) > 0)
	{
		enable = atoi(enable_result);
	}

	char *rule = get_value_change("filter.list.rule");

	if (enable == 1 && !strcmp(rule, "deny"))
	{
		while (1)
		{
			memset(cmd, 0, sizeof(cmd));
			snprintf(cmd, sizeof(cmd), "filter.@deny[%d]", count);
			char *value = get_value_change(cmd);
			if (char_has_value(value) == 0)
			{
				break;
			}

			memset(cmd, 0, sizeof(cmd));
			snprintf(cmd, sizeof(cmd), "filter.@deny[%d].mac", count);
			value = get_value_change(cmd);
			if (char_has_value(value) > 0)
			{
				if (!strcasecmp(mac, value))
				{
					access = 0;
					delete_mem(value);
					break;
				}
			}
			delete_mem(value);

			count++;
		}
	}
	delete_mem(enable_result);
	delete_mem(rule);
	return access;
}

/* 
--------------------------------------------------------------------------------
	函数功能：设置黑名单开关
--------------------------------------------------------------------------------
*/
int device_set_black(int sockfd, char *tagid, char *msg)
{
	int ret = 0;
	char data[1024];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		pValue = cJSON_GetObjectItem(pRoot, "bOpen");
		if (pValue != NULL && (pValue->type == cJSON_False || pValue->type == cJSON_True || pValue->type == cJSON_Number))
		{
			if (pValue->valueint == 1)
			{
				set_value("filter.list.enable", "1");
				set_value("filter.list.rule", "deny");

				system("lua /usr/lib/lua/luci/web/filter.lua 1 deny &");
			}
			else
			{
				set_value("filter.list.enable", "0");

				system("lua /usr/lib/lua/luci/web/filter.lua 0 deny &");
			}

			cJSON_AddNumberToObject(pRow, "code", 0);
			cJSON_AddStringToObject(pRow, "msg", "Success");
		}
		else
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "参数错误");
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_BLACK_STATUS_SET");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
	{
		cJSON_Delete(pRow);
	}

	if (pRoot != NULL)
	{
		cJSON_Delete(pRoot);
	}

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：获取黑名单列表
--------------------------------------------------------------------------------
*/
int device_get_blacklist(int sockfd, char *tagid)
{
	int ret = 0;
	int count = 0;
	int enable = 0;
	char rule[8];
	char cmd[128];
	char value[128];
	char data[1024];
	cJSON *pRow = NULL, *pRoot = NULL;

	pRow = cJSON_CreateArray();

	//Enable
	memset(value, 0, sizeof(value));
	ret = get_value("filter.list.enable", &value);
	if (ret == 1 && strlen(value))
		enable = atoi(value);

	//Rule
	memset(rule, 0, sizeof(rule));
	memset(value, 0, sizeof(value));
	ret = get_value("filter.list.rule", &value);
	if (ret == 1 && strlen(value))
		memcpy(rule, value, sizeof(rule));

	if (enable == 1 && !strcmp(rule, "deny"))
	{
		while (1)
		{
			memset(cmd, 0, sizeof(cmd));
			memset(value, 0, sizeof(value));
			snprintf(cmd, sizeof(cmd), "filter.@deny[%d]", count);
			ret = get_value(cmd, &value);
			if (ret == -1)
				break;

			cJSON_AddItemToArray(pRow, pRoot = cJSON_CreateObject());

			memset(cmd, 0, sizeof(cmd));
			memset(value, 0, sizeof(value));
			snprintf(cmd, sizeof(cmd), "filter.@deny[%d].mac", count);
			ret = get_value(cmd, &value);
			if (ret == 1 && strlen(value))
				cJSON_AddStringToObject(pRoot, "macAddress", value);

			memset(cmd, 0, sizeof(cmd));
			memset(value, 0, sizeof(value));
			snprintf(cmd, sizeof(cmd), "filter.@deny[%d].name", count);
			ret = get_value(cmd, &value);
			if (ret == 1 && strlen(value))
				cJSON_AddStringToObject(pRoot, "name", value);

			printf("### pRoot ### %s \n", cJSON_Print(pRoot));

			count++;
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "DEVICE_BLACKLIST_REP");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_BLACKLIST_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：添加黑名单
--------------------------------------------------------------------------------
*/
int device_add_blacklist(int sockfd, char *tagid, char *msg)
{
	int ret = 0;
	int flag = 0;
	int count = 0;
	int enable = 0;
	char rule[8];
	char cmd[128];
	char mac[20];
	char name[64];
	char value[128];
	char data[1024];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		memset(mac, 0, sizeof(mac));
		pValue = cJSON_GetObjectItem(pRoot, "macAddress");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(mac, pValue->valuestring, sizeof(mac));

		memset(name, 0, sizeof(name));
		pValue = cJSON_GetObjectItem(pRoot, "name");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(name, pValue->valuestring, sizeof(name));

		//Enable
		memset(value, 0, sizeof(value));
		ret = get_value("filter.list.enable", &value);
		if (ret == 1 && strlen(value))
			enable = atoi(value);

		//Rule
		memset(rule, 0, sizeof(rule));
		memset(value, 0, sizeof(value));
		ret = get_value("filter.list.rule", &value);
		if (ret == 1 && strlen(value))
			memcpy(rule, value, sizeof(rule));

		if (enable == 0 || strcmp(rule, "deny"))
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "添加黑名单,未开启黑名单模式");
		}
		else if (!strlen(mac))
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "添加黑名单,MAC地址为空");
		}
		else
		{
			while (1)
			{
				memset(cmd, 0, sizeof(cmd));
				memset(value, 0, sizeof(value));
				snprintf(cmd, sizeof(cmd), "filter.@deny[%d]", count);
				ret = get_value(cmd, &value);
				if (ret == -1)
				{
					printf("break\n");
					break;
				}

				memset(cmd, 0, sizeof(cmd));
				memset(value, 0, sizeof(value));
				snprintf(cmd, sizeof(cmd), "filter.@deny[%d].mac", count);
				ret = get_value(cmd, &value);
				if (ret == 1 && !strcasecmp(mac, value)) //修改
				{
					flag = 1;
					memset(cmd, 0, sizeof(cmd));
					snprintf(cmd, sizeof(cmd), "filter.@deny[%d].name", count);
					set_value(cmd, name);
				}

				count++;
			}

			if (flag == 0) //增加
			{
				uci_add("filter", "deny");

				memset(cmd, 0, sizeof(cmd));
				memset(value, 0, sizeof(value));
				snprintf(cmd, sizeof(cmd), "filter.@deny[%d].mac", count);
				set_value(cmd, mac);

				memset(cmd, 0, sizeof(cmd));
				snprintf(cmd, sizeof(cmd), "filter.@deny[%d].name", count);
				set_value(cmd, name);
			}

			system("lua /usr/lib/lua/luci/web/filter.lua 1 deny &");

			cJSON_AddNumberToObject(pRow, "code", 0);
			cJSON_AddStringToObject(pRow, "msg", "Success");
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_BLACKADD_SET");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：移除黑名单
--------------------------------------------------------------------------------
*/
int device_del_blacklist(int sockfd, char *tagid, char *msg)
{
	int ret = 0;
	int flag = 0;
	int count = 0;
	int enable = 0;
	char rule[8];
	char cmd[128];
	char mac[20];
	char name[64];
	char value[128];
	char data[1024];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		//Enable
		memset(value, 0, sizeof(value));
		ret = get_value("filter.list.enable", &value);
		if (ret == 1 && strlen(value))
			enable = atoi(value);

		//Rule
		memset(rule, 0, sizeof(rule));
		memset(value, 0, sizeof(value));
		ret = get_value("filter.list.rule", &value);
		if (ret == 1 && strlen(value))
			memcpy(rule, value, sizeof(rule));

		if (enable == 1 && !strcmp(rule, "deny"))
		{
			memset(name, 0, sizeof(name));
			pValue = cJSON_GetObjectItem(pRoot, "name");
			if (pValue != NULL && pValue->type == cJSON_String)
				memcpy(name, pValue->valuestring, sizeof(name));

			memset(mac, 0, sizeof(mac));
			pValue = cJSON_GetObjectItem(pRoot, "macAddress");
			if (pValue != NULL && pValue->type == cJSON_String)
				memcpy(mac, pValue->valuestring, sizeof(mac));

			while (1)
			{
				memset(cmd, 0, sizeof(cmd));
				memset(value, 0, sizeof(value));
				snprintf(cmd, sizeof(cmd), "filter.@deny[%d]", count);
				ret = get_value(cmd, &value);
				if (ret == -1)
					break;

				memset(cmd, 0, sizeof(cmd));
				memset(value, 0, sizeof(value));
				snprintf(cmd, sizeof(cmd), "filter.@deny[%d].mac", count);
				ret = get_value(cmd, &value);
				if (ret == 1 && !strcasecmp(mac, value)) //删除
				{
					flag = 1;
					memset(cmd, 0, sizeof(cmd));
					snprintf(cmd, sizeof(cmd), "uci delete filter.@deny[%d]", count);
					system(cmd);

					system("lua /usr/lib/lua/luci/web/filter.lua 1 deny &");

					cJSON_AddNumberToObject(pRow, "code", 0);
					cJSON_AddStringToObject(pRow, "msg", "Success");
				}

				count++;
			}

			if (flag == 0) //增加
			{
				cJSON_AddNumberToObject(pRow, "code", 1);
				cJSON_AddStringToObject(pRow, "msg", "黑名单删除失败");
			}
		}
		else
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "未开启黑名单模式");
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_BLACKREMOVE_SET");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：获取联网设备限速
--------------------------------------------------------------------------------
*/
int device_get_speedlimit(int sockfd, char *tagid, char *msg)
{
	int ret = 0;
	int flag = 0;
	int count = 0;
	char cmd[128];
	char ip[16];
	char mac[20];
	char value[128];
	char data[1024];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		memset(ip, 0, sizeof(ip));
		pValue = cJSON_GetObjectItem(pRoot, "ip");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(ip, pValue->valuestring, sizeof(ip));

		memset(mac, 0, sizeof(mac));
		pValue = cJSON_GetObjectItem(pRoot, "macAddress");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(mac, pValue->valuestring, sizeof(mac));

		if (strlen(ip) && strlen(mac))
		{
			//查询
			count = 0;
			while (1)
			{
				memset(cmd, 0, sizeof(cmd));
				memset(value, 0, sizeof(value));
				snprintf(cmd, sizeof(cmd), "speed.@limit[%d]", count);
				ret = get_value(cmd, &value);
				if (ret == -1)
					break;

				memset(cmd, 0, sizeof(cmd));
				memset(value, 0, sizeof(value));
				snprintf(cmd, sizeof(cmd), "speed.@limit[%d].mac", count);
				ret = get_value(cmd, &value);
				if (ret == 1 && (!strcasecmp(mac, value))) //修改
				{
					flag = 1;

					memset(cmd, 0, sizeof(cmd));
					memset(value, 0, sizeof(value));
					snprintf(cmd, sizeof(cmd), "speed.@limit[%d].uplimit", count);
					ret = get_value(cmd, &value);
					if (ret == 1)
						cJSON_AddNumberToObject(pRow, "up", atoi(value));
					else
						cJSON_AddNumberToObject(pRow, "up", 0);

					memset(cmd, 0, sizeof(cmd));
					memset(value, 0, sizeof(value));
					snprintf(cmd, sizeof(cmd), "speed.@limit[%d].downlimit", count);
					ret = get_value(cmd, &value);
					if (ret == 1)
						cJSON_AddNumberToObject(pRow, "down", atoi(value));
					else
						cJSON_AddNumberToObject(pRow, "down", 0);

					break;
				}
				count++;
			}

			if (flag == 0)
			{
				cJSON_AddNumberToObject(pRow, "up", 0);
				cJSON_AddNumberToObject(pRow, "down", 0);
			}
		}
		else
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "获取联网设备限速,参数错误");
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "DEVICE_LIMITSPEED_RSP");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_LIMITSPEED_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：联网设备限速
--------------------------------------------------------------------------------
*/
int device_set_speedlimit(int sockfd, char *tagid, char *msg)
{
	int ret = 0;
	int flag = 0;
	int count = 0;
	char cmd[128];
	char ip[16];
	char mac[20];
	char uplimit[8];
	char dwlimit[8];
	char value[128];
	char data[1024];
	cJSON *pRow = NULL, *pRank = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		memset(ip, 0, sizeof(ip));
		pValue = cJSON_GetObjectItem(pRoot, "ip");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(ip, pValue->valuestring, sizeof(ip));

		memset(mac, 0, sizeof(mac));
		pValue = cJSON_GetObjectItem(pRoot, "macAddress");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(mac, pValue->valuestring, sizeof(mac));

		pRank = cJSON_GetObjectItem(pRoot, "upAndDownSize");
		if (pRank == NULL)
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
		}
		else
		{
			memset(uplimit, 0, sizeof(uplimit));
			pValue = cJSON_GetObjectItem(pRank, "up");
			if (pValue != NULL && pValue->type == cJSON_Number)
				snprintf(uplimit, sizeof(uplimit), "%d", pValue->valueint);
			else
				memcpy(uplimit, "0", sizeof(uplimit));

			memset(dwlimit, 0, sizeof(dwlimit));
			pValue = cJSON_GetObjectItem(pRank, "down");
			if (pValue != NULL && pValue->type == cJSON_Number)
				snprintf(dwlimit, sizeof(dwlimit), "%d", pValue->valueint);
			else
				memcpy(dwlimit, "0", sizeof(dwlimit));
		}

		if (strlen(ip) && strlen(mac))
		{
			// 绑定MAC和IP
			cJSON_AddNumberToObject(pRow, "code", 0);
			cJSON_AddStringToObject(pRow, "msg", "Success");

			//查询,再设置
			while (1)
			{
				memset(cmd, 0, sizeof(cmd));
				memset(value, 0, sizeof(value));
				snprintf(cmd, sizeof(cmd), "speed.@limit[%d]", count);
				ret = get_value(cmd, &value);
				if (ret == -1)
					break;

				memset(cmd, 0, sizeof(cmd));
				memset(value, 0, sizeof(value));
				snprintf(cmd, sizeof(cmd), "speed.@limit[%d].mac", count);
				ret = get_value(cmd, &value);
				if (ret == 1 && (!strcasecmp(mac, value))) //修改
				{
					flag = 1;

					memset(cmd, 0, sizeof(cmd));
					snprintf(cmd, sizeof(cmd), "speed.@limit[%d].uplimit", count);
					set_value(cmd, uplimit);

					memset(cmd, 0, sizeof(cmd));
					snprintf(cmd, sizeof(cmd), "speed.@limit[%d].downlimit", count);
					set_value(cmd, dwlimit);
				}

				count++;
			}

			if (flag == 0) //增加
			{
				uci_add("speed", "limit");

				memset(cmd, 0, sizeof(cmd));
				snprintf(cmd, sizeof(cmd), "speed.@limit[%d].ip", count);
				set_value(cmd, ip);

				memset(cmd, 0, sizeof(cmd));
				snprintf(cmd, sizeof(cmd), "speed.@limit[%d].mac", count);
				set_value(cmd, mac);

				memset(cmd, 0, sizeof(cmd));
				snprintf(cmd, sizeof(cmd), "speed.@limit[%d].uplimit", count);
				set_value(cmd, uplimit);

				memset(cmd, 0, sizeof(cmd));
				snprintf(cmd, sizeof(cmd), "speed.@limit[%d].downlimit", count);
				set_value(cmd, dwlimit);
			}

			device_set_dhcp(ip, mac);

			system("/usr/sbin/speedlimit.sh start &");
		}
		else
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "联网设备限速,参数错误");
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_LIMITSPEED_SET");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRank != NULL)
		cJSON_Delete(pRank);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：绑定IP、MAC关系
--------------------------------------------------------------------------------
*/
int device_set_dhcp(char *ip, char *mac)
{
	int ret = 0;
	int flag = 0;
	int count = 0;
	char cmd[128];
	char value[128];

	while (1)
	{
		memset(cmd, 0, sizeof(cmd));
		memset(value, 0, sizeof(value));
		snprintf(cmd, sizeof(cmd), "dhcp.@host[%d]", count);
		ret = get_value(cmd, &value);
		if (ret == -1)
			break;

		memset(cmd, 0, sizeof(cmd));
		memset(value, 0, sizeof(value));
		snprintf(cmd, sizeof(cmd), "dhcp.@host[%d].mac", count);
		ret = get_value(cmd, &value);
		if (ret == 1 && (!strcasecmp(mac, value))) //修改
		{
			flag = 1;

			memset(cmd, 0, sizeof(cmd));
			snprintf(cmd, sizeof(cmd), "dhcp.@host[%d].ip", count);
			set_value(cmd, ip);
		}

		count++;
	}

	if (flag == 0)
	{
		uci_add("dhcp", "host");

		memset(cmd, 0, sizeof(cmd));
		snprintf(cmd, sizeof(cmd), "dhcp.@host[%d].ip", count);
		set_value(cmd, ip);

		memset(cmd, 0, sizeof(cmd));
		snprintf(cmd, sizeof(cmd), "dhcp.@host[%d].mac", count);
		set_value(cmd, mac);
	}

	system("/etc/init.d/dnsmasq reload &");
}

/* 
--------------------------------------------------------------------------------
	函数功能：后台升级路由器
--------------------------------------------------------------------------------
*/
int device_version_upgrade(int sockfd, char *tagid, char *msg)
{
	printf("upload by remote %s \n", msg);
	cJSON *pRoot = NULL, *pRow = NULL, *pValue = NULL;
	pRow = cJSON_CreateObject();
	char url[100];
	char result[200];
	char md5[100];
	char version[100];
	char versionurl[1000];

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		memset(url, 0, sizeof(url));
		pValue = cJSON_GetObjectItem(pRoot, "url");
		if (pValue != NULL && pValue->type == cJSON_String)
		{
			memcpy(url, pValue->valuestring, sizeof(url));
		}

		memset(md5, 0, sizeof(md5));
		pValue = cJSON_GetObjectItem(pRoot, "md5");
		if (pValue != NULL && pValue->type == cJSON_String)
		{
			memcpy(md5, pValue->valuestring, sizeof(md5));
		}

		memset(version, 0, sizeof(version));
		pValue = cJSON_GetObjectItem(pRoot, "version");
		if (pValue != NULL && pValue->type == cJSON_String)
		{
			memcpy(version, pValue->valuestring, sizeof(version));
		}

		memset(result, 0, sizeof(result));
		memset(versionurl, 0, sizeof(result));
		snprintf(versionurl, sizeof(versionurl), "curl http://192.168.188.1/cgi-bin/luci/file/upload/remoteupgrade?url=%s\\&md5=%s\\&version=%s", url, md5, version);
		printf("versionurl:%s\n", versionurl);
		shell_cmd(versionurl, result);
		printf("result:%s\n", result);
	}

	return 0;
}
